clear all
set maxvar 10000
set more off

* set up the working directory in your computer
cd 

* load data 
use daughter_analytic, clear

replace c_educ = 20 if c_educ > 20 & ~missing(c_educ)

* variable missing controler; n_nonmiss 
local cv1 c_female c_age c_born x_oldage c_educ
local cv2 c_educ c_sei
egen n_nonmiss = rowmiss(`cv1')

* keep only relevant samples : out_sample == 100
* Thus, we use n_nonmiss and out_sample for selecting the analytic sample in analysis

* =============================================================
* Table S1
* =============================================================

table out_sample if cnum == 36
table out_sample if cnum == 13
table out_sample if dataset == "ESS"

* =============================================================
* Table 1 : independent t-tests for each covariates
* =============================================================
gen v_name = ""
gen v_sample = ""
gen v_diff = .
gen v_t = .
gen v_p = .

local n = 1
foreach X of varlist c_female c_age c_born x_oldage c_educ {
reg `X' fx_daughter i.year if cnum == 36 & out_sample == 100 & n_nonmiss == 0
replace v_sample = "US" in `n'
replace v_name = "`X'" in `n'
replace v_diff = _b[fx_daughter] in `n'
replace v_t = _b[fx_daughter]/_se[fx_daughter] in `n'
replace v_p = (2 * ttail(e(df_r), abs(_b[fx_daughter]/_se[fx_daughter]))) in `n' 
local n = `n'+1
}
foreach X of varlist c_female c_age c_born x_oldage c_educ {
reg `X' fx_daughter i.essround if cnum == 13 & out_sample == 100 & n_nonmiss == 0
replace v_sample = "UK" in `n'
replace v_name = "`X'" in `n'
replace v_diff = _b[fx_daughter] in `n'
replace v_t = _b[fx_daughter]/_se[fx_daughter] in `n'
replace v_p = (2 * ttail(e(df_r), abs(_b[fx_daughter]/_se[fx_daughter]))) in `n' 
local n = `n'+1
}
foreach X of varlist c_female c_age c_born x_oldage c_educ {
reg `X' fx_daughter i.essround i.cnum if cnum != 13 & out_sample == 100 & n_nonmiss == 0
replace v_sample = "ESS" in `n'
replace v_name = "`X'" in `n'
replace v_diff = _b[fx_daughter] in `n'
replace v_t = _b[fx_daughter]/_se[fx_daughter] in `n'
replace v_p = (2 * ttail(e(df_r), abs(_b[fx_daughter]/_se[fx_daughter]))) in `n' 
local n = `n'+1
}

outsheet v_* using T1_balance.csv, comma replace, if v_name != ""

* =============================================================
* Table S3 : replication and extention of Hopfcroft(2005)'s analysis
* =============================================================

gen c_male = 1-c_female
gen c_sei100 = c_sei/100
label var c_sei100 "SEI index"
label var c_male "Parental Sex(=1 male)"

eststo clear
eststo: reg px_bdaughter c_male##c.c_sei100 if cnum == 36 & year == 1994
eststo: reg fx_bdaughter c_male##c.c_sei100 if cnum == 36 & year == 1994
eststo: reg px_daughter c_male##c.c_sei100 i.year if cnum == 36 & out_sample == 100
eststo: reg fx_daughter c_male##c.c_sei100 i.year if cnum == 36 & out_sample == 100
eststo: reg px_daughter c_male##c.c_sei100 i.year if cnum != 36 & out_sample == 100
eststo: reg fx_daughter c_male##c.c_sei100 i.year if cnum != 36 & out_sample == 100
esttab * using replicate_hopfcraft.csv, csv replace star(+ 0.1 * 0.05 ** 0.01) b(%9.2f) r2 nogap label drop(*year) se


* =============================================================
* Figure S1 : Repeat the analysis in each year 
* =============================================================
sort year
egen ry = group(year) if out_sample == 100 & dataset == "GSS" & n_nonmiss == 0 & ~missing(c_sei100)

gen c_maleXc_sei100 = c_male*c_sei100


** LOOP over each year in each sex : main effects
gen o_year = .
gen o_coef = .
gen o_se = .
gen o_p = .
gen o_star =""
gen o_dv = ""
gen o_female = .

local n = 1
foreach DV of varlist fx_daughter px_daughter {
tab year if out_sample == 100 & dataset == "GSS" & n_nonmiss == 0 & ~missing(c_sei100)
local ymax = `r(r)'
	forvalues i = 1/`ymax'{
	replace o_dv = "`DV'" in `n'
	replace o_female = 0 in `n'
	sum year if ry == `i' & dataset == "GSS" 
	replace o_year = `r(mean)' in `n'
	reg  `DV' c_male c_sei100 if out_sample == 100 & ry == `i' & cnum == 36 & n_nonmiss == 0
	replace o_coef = _b[c_sei100] in `n'
	replace o_se = _se[c_sei100] in `n'
	replace o_p = 2 * ttail(e(df_r), abs(_b[c_sei100]/_se[c_sei100])) in `n'
	local n = `n'+1
	}
	forvalues i = 1/`ymax'{
	replace o_dv = "`DV'" in `n'
	replace o_female = 1 in `n'
	sum year if ry == `i' & dataset == "GSS" 
	replace o_year = `r(mean)' in `n'
	reg  `DV' c_sei100 if out_sample == 100 & ry == `i' & cnum == 36 & n_nonmiss == 0 & c_male == 0
	replace o_coef = _b[c_sei100] in `n'
	replace o_se = _se[c_sei100] in `n'
	replace o_p = 2 * ttail(e(df_r), abs(_b[c_sei100]/_se[c_sei100])) in `n'
	local n = `n'+1
	}
	forvalues i = 1/`ymax'{
	replace o_dv = "`DV'" in `n'
	replace o_female = 2 in `n'
	sum year if ry == `i' & dataset == "GSS" 
	replace o_year = `r(mean)' in `n'
	reg  `DV' c_sei100 if out_sample == 100 & ry == `i' & cnum == 36 & n_nonmiss == 0 & c_male == 1
	replace o_coef = _b[c_sei100] in `n'
	replace o_se = _se[c_sei100] in `n'
	replace o_p = 2 * ttail(e(df_r), abs(_b[c_sei100]/_se[c_sei100])) in `n'
	local n = `n'+1
	}
}
replace o_star = "+" if o_p < 0.1
replace o_star = "*" if o_p < 0.05
outsheet o_* using year_LPM_sei_main.csv, comma replace, if o_coef != .
drop o_*


** LOOP over each year : interaction terms
gen o_year = .
gen o_coef = .
gen o_se = .
gen o_p = .
gen o_star =""
gen o_dv = ""

local n = 1
foreach DV of varlist fx_daughter px_daughter {
tab year if out_sample == 100 & dataset == "GSS" & n_nonmiss == 0 & ~missing(c_sei100)
local ymax = `r(r)'
	forvalues i = 1/`ymax'{
	replace o_dv = "`DV'" in `n'
	sum year if ry == `i' & dataset == "GSS" 
	replace o_year = `r(mean)' in `n'
	reg  `DV' c_male c_sei100 c_maleXc_sei100 if out_sample == 100 & ry == `i' & cnum == 36 & n_nonmiss == 0
	replace o_coef = _b[c_maleXc_sei100] in `n'
	replace o_se = _se[c_maleXc_sei100] in `n'
	replace o_p = 2 * ttail(e(df_r), abs(_b[c_maleXc_sei100]/_se[c_maleXc_sei100])) in `n'
	local n = `n'+1
	}
}
replace o_star = "+" if o_p < 0.1
replace o_star = "*" if o_p < 0.05
outsheet o_* using year_LPM_sei.csv, comma replace, if o_coef != .
drop o_*


* =============================================================
* Table S4 : Descriptive Statistics which will produce Figure 1 
* NOTE : Figure 1 is created by using microsoft-excel;
* =============================================================

local d_dv d_repscale d_dem d_rep d_conscale100 
local d_iv fx_daughter nx_child x_hsize 
local d_cv x_oldage c_age c_female c_born c_educ

gen t3_gr = ""
gen t3_name = ""
gen t3_tmean = .
gen t3_tsd = .
gen t3_tN =.
gen t3_amean = .
gen t3_asd = .
gen t3_aN = .
gen t3_p = .

local n = 1
foreach X of varlist `d_dv' `d_iv' `d_cv' {
replace t3_gr = "US" in `n'
replace t3_name = "`X'" in `n'
sum `X' if cnum == 36 
replace t3_tmean = r(mean) in `n'
replace t3_tsd = r(sd) in `n'
replace t3_tN = r(N) in `n'
sum `X' if cnum == 36 & out_sample == 100
replace t3_amean = r(mean) in `n'
replace t3_asd = r(sd) in `n'
replace t3_aN = r(N) in `n'
local n = `n'+1
}

foreach X of varlist `d_dv' `d_iv' `d_cv' {
replace t3_gr = "UK" in `n'
replace t3_name = "`X'" in `n'
sum `X' if cnum == 13
replace t3_tmean = r(mean) in `n'
replace t3_tsd = r(sd) in `n'
replace t3_tN = r(N) in `n'
sum `X' if cnum == 13 & out_sample == 100
replace t3_amean = r(mean) in `n'
replace t3_asd = r(sd) in `n'
replace t3_aN = r(N) in `n'
local n = `n'+1
}

foreach X of varlist `d_dv' `d_iv' `d_cv' {
replace t3_gr = "ESS" in `n'
replace t3_name = "`X'" in `n'
sum `X' if dataset == "ESS"
replace t3_tmean = r(mean) in `n'
replace t3_tsd = r(sd) in `n'
replace t3_tN = r(N) in `n'
sum `X' if dataset == "ESS" & out_sample == 100
replace t3_amean = r(mean) in `n'
replace t3_asd = r(sd) in `n'
replace t3_aN = r(N) in `n'
local n = `n'+1
}

gen t3_diff = t3_tmean-t3_amean
gen t3_diff_sd = t3_diff / t3_tsd
gen t3_se = sqrt(t3_tsd^2/t3_tN + t3_asd^2/t3_aN)
gen t3_t = (t3_tmean-t3_amean) / t3_se
gen t3_df = (t3_tN+t3_aN-2)
gen t3_2p = 2*ttail(t3_df,abs(t3_t))
gen t3_sig = "*" if t3_2p < 0.05

* exporting data out for Appendix Table
outsheet t3_gr t3_name t3_tmean t3_tN t3_amean t3_aN if t3_gr != "" using TS4_descriptive.csv, comma replace
* exporting data out for Plotting Figure 1 
outsheet t3_gr t3_name t3_diff_sd if t3_gr != "" using TS4_figure.csv, comma replace


* =============================================================
* Table 2 / Table S5 : Replication Analysis : effects of having the oldest daughter on party ID : US / UK 
* =============================================================

* Replication : US
estimates clear
local cv1 c_female c_age c_born x_oldage c_educ
reg d_repscale fx_daughter i.year if cnum == 36 & out_sample == 100 & n_nonmiss == 0
estimates store m1
reg d_repscale fx_daughter `cv1' i.year if cnum ==36 & out_sample == 100 & n_nonmiss == 0
estimates store m2
reg d_rep fx_daughter i.year if cnum == 36 & out_sample == 100 & n_nonmiss == 0
estimates store m3
reg d_rep fx_daughter `cv1' i.year if cnum == 36 & out_sample == 100 & n_nonmiss == 0
estimates store m4
ologit d_repscale fx_daughter i.year if cnum == 36 & out_sample == 100 & n_nonmiss == 0
estimates store m5
ologit d_repscale fx_daughter `cv1' i.year if cnum == 36 & out_sample == 100 & n_nonmiss == 0
estimates store m6
logit d_rep fx_daughter i.year if cnum == 36 & out_sample == 100 & n_nonmiss == 0
estimates store m7
logit d_rep fx_daughter `cv1' i.year if cnum == 36 & out_sample == 100 & n_nonmiss == 0
estimates store m8
esttab * using T2_US.csv, csv replace star(+ 0.1 * 0.05) nogap ar2 pr2 se b(%9.3f) keep(fx_daughter)

* Replication : UK
estimates clear
local cv1 c_female c_age c_born x_oldage c_educ
reg d_repscale fx_daughter i.essround if cnum == 13 & out_sample == 100 & n_nonmiss == 0
estimates store m1
reg d_repscale fx_daughter `cv1' i.essround if cnum == 13 & out_sample == 100 & n_nonmiss == 0
estimates store m2
reg d_rep fx_daughter i.essround if cnum == 13 & out_sample == 100 & n_nonmiss == 0
estimates store m3
reg d_rep fx_daughter `cv1' i.essround if cnum == 13 & out_sample == 100 & n_nonmiss == 0
estimates store m4
ologit d_repscale fx_daughter i.essround if cnum == 13 & out_sample == 100 & n_nonmiss == 0
estimates store m5
ologit d_repscale fx_daughter `cv1' i.essround if cnum == 13 & out_sample == 100 & n_nonmiss == 0
estimates store m6
logit d_rep fx_daughter i.essround if cnum == 13 & out_sample == 100 & n_nonmiss == 0
estimates store m7
logit d_rep fx_daughter `cv1' i.essround if cnum == 13 & out_sample == 100 & n_nonmiss == 0
estimates store m8
esttab * using T2_UK.csv, csv replace star(+ 0.1 * 0.05) nogap ar2 pr2  se b(%9.2f) keep(fx_daughter) 

* Table S3 : overlapping period in UK
estimates clear
local cv1 c_female c_age c_born x_oldage c_educ
reg d_repscale fx_daughter i.essround if cnum == 13 & out_sample == 100 & n_nonmiss == 0 & essround < 3
estimates store m1
reg d_repscale fx_daughter `cv1' i.essround if cnum == 13 & out_sample == 100 & n_nonmiss == 0 & essround < 3
estimates store m2
reg d_rep fx_daughter i.essround if cnum == 13 & out_sample == 100 & n_nonmiss == 0 & essround < 3
estimates store m3
reg d_rep fx_daughter `cv1' i.essround if cnum == 13 & out_sample == 100 & n_nonmiss == 0 & essround < 3
estimates store m4
ologit d_repscale fx_daughter i.essround if cnum == 13 & out_sample == 100 & n_nonmiss == 0 & essround < 3
estimates store m5
ologit d_repscale fx_daughter `cv1' i.essround if cnum == 13 & out_sample == 100 & n_nonmiss == 0 & essround < 3
estimates store m6
logit d_rep fx_daughter i.essround if cnum == 13 & out_sample == 100 & n_nonmiss == 0 & essround < 3
estimates store m7
logit d_rep fx_daughter `cv1' i.essround if cnum == 13 & out_sample == 100 & n_nonmiss == 0 & essround < 3
estimates store m8
esttab * using T2_UK_overlap.csv, csv replace star(+ 0.1 * 0.05) nogap ar2 pr2  se b(%9.2f) keep(fx_daughter) 

* =============================================================
* Table 3 / Table S6 : Extended Analysis on Political Ideology
* =============================================================

* GSS 1994
cap: drop n_nonmiss2
egen n_nonmiss2 = rowmiss(c_female c_age c_born x_bageoldest c_educ)
cap: drop d_con
gen d_con = (d_conscale > 0) if ~missing(d_conscale)
label var d_con "Conservative"

estimates clear
local cv1 c_female c_age c_born x_bageoldest c_educ
reg d_conscale100 fx_bdaughter i.year if cnum == 36 & n_nonmiss2 == 0 & year == 1994
estimates store m1
reg d_conscale100 fx_bdaughter `cv1' i.year if cnum ==36 & n_nonmiss2 == 0 & year == 1994
estimates store m2
reg d_con fx_bdaughter i.year if cnum == 36 & n_nonmiss2 == 0 & year == 1994
estimates store m3
reg d_con fx_bdaughter `cv1' i.year if cnum == 36 & n_nonmiss2 == 0 & year == 1994
estimates store m4
ologit d_conscale fx_bdaughter i.year if cnum == 36 & n_nonmiss2 == 0 & year == 1994
estimates store m5
ologit d_conscale fx_bdaughter `cv1' i.year if cnum == 36 & n_nonmiss2 == 0 & year == 1994
estimates store m6
logit d_con fx_bdaughter i.year if cnum == 36 & n_nonmiss2 == 0 & year == 1994
estimates store m7
logit d_con fx_bdaughter `cv1' i.year if cnum == 36 & n_nonmiss2 == 0 & year == 1994
estimates store m8
esttab * using T3_GSS_1994.csv, csv replace star(+ 0.1 * 0.05) nogap ar2 pr2 se b(%9.2f) keep(fx_bdaughter)

* the entire GSS
estimates clear
local cv1 c_female c_age c_born x_oldage c_educ
reg d_conscale100 fx_daughter i.year if cnum == 36 & out_sample == 100 & n_nonmiss == 0
estimates store m1
reg d_conscale100 fx_daughter `cv1' i.year if cnum ==36 & out_sample == 100 & n_nonmiss == 0
estimates store m2
reg d_con fx_daughter i.year if cnum == 36 & out_sample == 100 & n_nonmiss == 0
estimates store m3
reg d_con fx_daughter `cv1' i.year if cnum == 36 & out_sample == 100 & n_nonmiss == 0
estimates store m4
ologit d_conscale fx_daughter i.year if cnum == 36 & out_sample == 100 & n_nonmiss == 0
estimates store m5
ologit d_conscale fx_daughter `cv1' i.year if cnum == 36 & out_sample == 100 & n_nonmiss == 0
estimates store m6
logit d_con fx_daughter i.year if cnum == 36 & out_sample == 100 & n_nonmiss == 0
estimates store m7
logit d_con fx_daughter `cv1' i.year if cnum == 36 & out_sample == 100 & n_nonmiss == 0
estimates store m8
esttab * using T3_GSS.csv, csv replace star(+ 0.1 * 0.05) nogap ar2 pr2 se b(%9.2f) keep(fx_daughter)

* the entire ESS
estimates clear
local cv1 c_female c_age c_born x_oldage c_educ
reg d_conscale100 fx_daughter i.essround i.cnum if cnum != 36 & out_sample == 100 & n_nonmiss == 0
estimates store m1
reg d_conscale100 fx_daughter `cv1' i.essround i.cnum if cnum != 36 & out_sample == 100 & n_nonmiss == 0
estimates store m2
reg d_con fx_daughter i.essround i.cnum if cnum != 36 & out_sample == 100 & n_nonmiss == 0
estimates store m3
reg d_con fx_daughter `cv1' i.essround i.cnum if cnum != 36 & out_sample == 100 & n_nonmiss == 0
estimates store m4
ologit d_conscale fx_daughter i.essround i.cnum if cnum != 36 & out_sample == 100 & n_nonmiss == 0
estimates store m5
ologit d_conscale fx_daughter `cv1' i.essround i.cnum if cnum != 36 & out_sample == 100 & n_nonmiss == 0
estimates store m6
logit d_con fx_daughter i.essround i.cnum if cnum != 36 & out_sample == 100 & n_nonmiss == 0
estimates store m7
logit d_con fx_daughter `cv1' i.essround i.cnum if cnum != 36 & out_sample == 100 & n_nonmiss == 0
estimates store m8
esttab * using T3_ESS.csv, csv replace star(+ 0.1 * 0.05) nogap ar2 pr2  se b(%9.3f) keep(fx_daughter) 



* =============================================================
* Table S7 : mtmixed : multilevel models
* =============================================================
drop v_*

preserve

* Here, we estimate four models
keep if out_sample == 100
drop if cnum == 36
drop if n_nonmiss != 0

* Generate variables to store the results
gen v_model = ""
gen v_name = ""
gen v_coef = .
gen v_se = .

* Random intercept without controls
xtmixed d_conscale100 fx_daughter || cnum: , mle
estimates store m1
local n = 1
replace v_model = "ri without countrol" in `n'
replace v_name = "fx_daughter" in `n'
replace v_coef = _b[fx_daughter] in `n'
replace v_se = _se[fx_daughter] in `n'
local n = `n'+1
replace v_model = "ri without countrol" in `n'
replace v_name = "_cons" in `n'
replace v_coef = _b[_cons] in `n'
replace v_se = _se[_cons] in `n'
local n = `n'+1
replace v_model = "ri without countrol" in `n'
replace v_name = "sd11" in `n'
_diparm lns1_1_1, f(exp(@)) d(exp(@))
replace v_coef = r(est) in `n'
replace v_se = r(se) in `n'
local n = `n'+1
replace v_model = "ri without countrol" in `n'
replace v_name = "sd(r)" in `n'
_diparm lnsig_e, f(exp(@)) d(exp(@))
replace v_coef = r(est) in `n'
replace v_se = r(se) in `n'
local n = `n'+1
replace v_model = "ri without countrol" in `n'
replace v_name = "N" in `n'
replace v_coef = e(N) in `n'
local n = `n'+1
replace v_model = "ri without countrol" in `n'
replace v_name = "LL" in `n'
replace v_coef = e(ll) in `n'

* Random intercept with controls
xtmixed d_conscale100 fx_daughter c_female c_age c_born x_oldage c_educ i.essround || cnum: , mle
estimates store m2
local n = `n'+1
replace v_model = "ri with countrol" in `n'
replace v_name = "fx_daughter" in `n'
replace v_coef = _b[fx_daughter] in `n'
replace v_se = _se[fx_daughter] in `n'
local n = `n'+1
replace v_model = "ri with countrol" in `n'
replace v_name = "_cons" in `n'
replace v_coef = _b[_cons] in `n'
replace v_se = _se[_cons] in `n'
local n = `n'+1
replace v_model = "ri with countrol" in `n'
replace v_name = "sd11" in `n'
_diparm lns1_1_1, f(exp(@)) d(exp(@))
replace v_coef = r(est) in `n'
replace v_se = r(se) in `n'
local n = `n'+1
replace v_model = "ri with countrol" in `n'
replace v_name = "sd(r)" in `n'
_diparm lnsig_e, f(exp(@)) d(exp(@))
replace v_coef = r(est) in `n'
replace v_se = r(se) in `n'
local n = `n'+1
replace v_model = "ri with countrol" in `n'
replace v_name = "N" in `n'
replace v_coef = e(N) in `n'
local n = `n'+1
replace v_model = "ri with countrol" in `n'
replace v_name = "LL" in `n'
replace v_coef = e(ll) in `n'

* Random Coefficient without controls
xtmixed d_conscale100 fx_daughter c_female c_age c_born x_oldage c_educ i.essround || cnum: fx_daughter , covariance(unstructure) mle
estimates store m3
local n = `n'+1
replace v_model = "rc with countrol" in `n'
replace v_name = "fx_daughter" in `n'
replace v_coef = _b[fx_daughter] in `n'
replace v_se = _se[fx_daughter] in `n'
local n = `n'+1
replace v_model = "rc with countrol" in `n'
replace v_name = "_cons" in `n'
replace v_coef = _b[_cons] in `n'
replace v_se = _se[_cons] in `n'
local n = `n'+1

replace v_model = "rc with countrol" in `n'
replace v_name = "sd11" in `n'
_diparm lns1_1_1, f(exp(@)) d(exp(@))
replace v_coef = r(est) in `n'
replace v_se = r(se) in `n'

local n = `n'+1
replace v_model = "rc with countrol" in `n'
replace v_name = "sd22" in `n'
_diparm lns1_1_2, f(exp(@)) d(exp(@))
replace v_coef = r(est) in `n'
replace v_se = r(se) in `n'
local n = `n'+1
replace v_model = "rc with countrol" in `n'
replace v_name = "cor12" in `n'
_diparm atr1_1_1_2, f(tanh(@)) d(1-tanh(@)^2)
replace v_coef = r(est) in `n'
replace v_se = r(se) in `n'
local n = `n'+1
replace v_model = "rc with countrol" in `n'
replace v_name = "sd(r)" in `n'
_diparm lnsig_e, f(exp(@)) d(exp(@))
replace v_coef = r(est) in `n'
replace v_se = r(se) in `n'
local n = `n'+1
replace v_model = "rc with countrol" in `n'
replace v_name = "N" in `n'
replace v_coef = e(N) in `n'
local n = `n'+1
replace v_model = "rc with countrol" in `n'
replace v_name = "LL" in `n'
replace v_coef = e(ll) in `n'

* Conduct LR test on the zero variance of the slope coefficients
lrtest m2 m3
local n = `n'+1
replace v_model = "rc with countrol" in `n'
replace v_name = "Test result (chi2 and P)" in `n'
replace v_coef = r(chi2) in `n'
replace v_se = 0.5*chi2tail(1, r(chi2)) + 0.5*chi2tail(2,r(chi2)) in `n'

outsheet v_* if v_name != "" using TS7_ML_analysis.csv, comma replace
restore

* =============================================================
* Figure 2 : Period Variations on party id in the US
* =============================================================
cap: drop ry
sort year
egen ry = group(year) if out_sample == 100 & dataset == "GSS" & n_nonmiss == 0

** LOOP
gen o_year = .
gen o_coef = .
gen o_se = .
gen o_p = .
gen o_star =""
gen o_dv = ""

local n = 1
foreach X of varlist fx_daughter {
foreach DV of varlist d_repscale d_conscale {
tab year if out_sample == 100 & dataset == "GSS" & n_nonmiss == 0
local ymax = `r(r)'
	forvalues i = 1/`ymax'{
	replace o_dv = "`DV'" in `n'
	sum year if ry == `i' & dataset == "GSS" 
	replace o_year = `r(mean)' in `n'
	reg  `DV' `X' if out_sample == 100 & ry == `i' & cnum == 36 & n_nonmiss == 0
	replace o_coef = _b[`X'] in `n'
	replace o_se = _se[`X'] in `n'
	replace o_p = 2 * ttail(e(df_r), abs(_b[`X']/_se[`X'])) in `n'
	local n = `n'+1
	}
}
replace o_star = "+" if o_p < 0.1
replace o_star = "*" if o_p < 0.05
outsheet o_* using year_LPM_`X'.csv, comma replace, if o_coef != .
}



* =============================================================
* Figure 3 : Country Variations on political ideology
* =============================================================

local dv d_conscale100
local iv fx_daughter

gen q_weight = ""
gen q_control = ""
gen q_vname = ""
gen q_cntry = .

gen q_N =.

gen q_coef =.
gen q_se =.
gen q_lci = .
gen q_uci = .
gen q_p = .

local n = 1
* without control + without survey weights
local cv 
forvalues c = 1/36 {
qui reg `dv' `iv' `cv' i.period if cnum == `c' & n_nonmiss == 0 & out_sample == 100
replace q_weight = "noweight" in `n'
replace q_control = "nocontrol" in `n'
replace q_cntry = `c' in `n'
replace q_vname = "`iv'" in `n'
replace q_N = e(N) in `n'

replace q_coef = _b[`iv'] in `n'
replace q_se = _se[`iv'] in `n'
replace q_lci = _b[`iv'] - invttail(e(df_r),0.05/2)*_se[`iv'] in `n'
replace q_uci = _b[`iv'] + invttail(e(df_r),0.05/2)*_se[`iv'] in `n'
replace q_p = 2 * ttail(e(df_r), abs(_b[`iv']/_se[`iv'])) in `n'
local n = `n'+1
}

* with control + without survey weight
local cv c_female c_age c_born x_oldage c_educ
forvalues c = 1/36 {
qui reg `dv' `iv' `cv' i.period if cnum == `c' & n_nonmiss == 0 & out_sample == 100
replace q_weight = "noweight" in `n'
replace q_control = "control" in `n'
replace q_cntry = `c' in `n'
replace q_vname = "`iv'" in `n'
replace q_N = e(N) in `n'

replace q_coef = _b[`iv'] in `n'
replace q_se = _se[`iv'] in `n'
replace q_lci = _b[`iv'] - invttail(e(df_r),0.05/2)*_se[`iv'] in `n'
replace q_uci = _b[`iv'] + invttail(e(df_r),0.05/2)*_se[`iv'] in `n'
replace q_p = 2 * ttail(e(df_r), abs(_b[`iv']/_se[`iv'])) in `n'
local n = `n'+1
}


* with control + with survey weight
local cv c_female c_age c_born x_oldage c_educ
forvalues c = 1/36 {
qui reg `dv' `iv' `cv' i.period [aw=wt] if cnum == `c' & n_nonmiss == 0 & out_sample == 100
replace q_weight = "weight" in `n'
replace q_control = "control" in `n'
replace q_cntry = `c' in `n'
replace q_vname = "`iv'" in `n'
replace q_N = e(N) in `n'

replace q_coef = _b[`iv'] in `n'
replace q_se = _se[`iv'] in `n'
replace q_lci = _b[`iv'] - invttail(e(df_r),0.05/2)*_se[`iv'] in `n'
replace q_uci = _b[`iv'] + invttail(e(df_r),0.05/2)*_se[`iv'] in `n'
replace q_p = 2 * ttail(e(df_r), abs(_b[`iv']/_se[`iv'])) in `n'
local n = `n'+1
}

gen q_country = ""
replace q_country = "Austria" if q_cntry ==1
replace q_country = "Belgium" if q_cntry ==2
replace q_country = "Bulgaria" if q_cntry ==3
replace q_country = "Switzerland" if q_cntry ==4
replace q_country = "Cyprus" if q_cntry ==5
replace q_country = "Czech Republic" if q_cntry ==6
replace q_country = "Germany" if q_cntry ==7
replace q_country = "Denmark" if q_cntry ==8
replace q_country = "Estonia" if q_cntry ==9
replace q_country = "Spain" if q_cntry ==10
replace q_country = "Finland" if q_cntry ==11
replace q_country = "France" if q_cntry ==12
replace q_country = "United Kingdom" if q_cntry ==13
replace q_country = "Greece" if q_cntry ==14
replace q_country = "Croatia" if q_cntry ==15
replace q_country = "Hungary" if q_cntry ==16
replace q_country = "Ireland" if q_cntry ==17
replace q_country = "Israel" if q_cntry ==18
replace q_country = "Iceland" if q_cntry ==19
replace q_country = "Italia" if q_cntry ==20
replace q_country = "Lithuania" if q_cntry ==21
replace q_country = "Luxemburg" if q_cntry ==22
replace q_country = "Latvia" if q_cntry ==23
replace q_country = "Nethelands" if q_cntry ==24
replace q_country = "Norway" if q_cntry ==25
replace q_country = "Poland" if q_cntry ==26
replace q_country = "Portugal" if q_cntry ==27
replace q_country = "Romania" if q_cntry ==28
replace q_country = "Russian Federation" if q_cntry ==29
replace q_country = "Sweden" if q_cntry ==30
replace q_country = "Slovenia" if q_cntry ==31
replace q_country = "Slovakia" if q_cntry ==32
replace q_country = "Turkey" if q_cntry ==33
replace q_country = "Ukraine" if q_cntry ==34
replace q_country = "Kosovo" if q_cntry ==35
replace q_country = "United States" if q_cntry ==36

outsheet q_* using `dv'_`iv'_figure3.csv, comma replace, if q_vname != ""


* ============================================================
* Table S9/S10 : Interaction Effects using linear interaction 
* ============================================================

gen c_femalexf = fx_daughter*c_female
gen c_agexf = fx_daughter*c_age
gen c_bornxf = fx_daughter*c_born
gen x_oldagexf = fx_daughter*x_oldage
gen c_educxf = fx_daughter*c_educ

label var c_femalexf "Female * Oldest Daughter"
label var c_agexf "Age * Oldest Daughter"
label var c_bornxf "Native-born * Oldest Daughter"
label var x_oldagexf "Oldest's Age * Oldest Daughter"
label var c_educxf "Years of Education * Oldest Daughter"

local cv1 c_female c_age c_born x_oldage c_educ
estimates clear
local i = 0
foreach X of varlist c_female c_age c_born x_oldage c_educ {
local i = `i'+1
reg d_repscale fx_daughter `cv1' `X'xf i.essround if cnum == 13 & out_sample == 100 & n_nonmiss == 0
estimates store uk`i'
}
esttab * using interaction_rep_UK.csv, csv replace star(+ 0.1 * 0.05) nogap ar2 pr2  se b(%9.3f) drop(*essround)  label

local cv1 c_female c_age c_born x_oldage c_educ
estimates clear
local i = 0
foreach X of varlist c_female c_age c_born x_oldage c_educ {
local i = `i'+1
reg d_repscale fx_daughter `cv1' `X'xf i.year if cnum == 36 & out_sample == 100 & n_nonmiss == 0
estimates store us`i'
}
esttab * using interaction_rep_US.csv, csv replace star(+ 0.1 * 0.05) nogap ar2 pr2  se b(%9.3f) drop(*year)  label

local cv1 c_female c_age c_born x_oldage c_educ
estimates clear
local i = 0
foreach X of varlist c_female c_age c_born x_oldage c_educ {
local i = `i'+1
reg d_conscale100 fx_daughter `cv1' `X'xf i.year if cnum == 36 & out_sample == 100 & n_nonmiss == 0
estimates store gss`i'
}
esttab * using interaction_con_GSS.csv, csv replace star(+ 0.1 * 0.05) nogap ar2 pr2  se b(%9.2f) drop(*year)  label


local cv1 c_female c_age c_born x_oldage c_educ
estimates clear
local i = 0
foreach X of varlist c_female c_age c_born x_oldage c_educ {
local i = `i'+1
reg d_conscale100 fx_daughter `cv1' `X'xf i.essround if cnum != 36 & out_sample == 100 & n_nonmiss == 0
estimates store ess`i'
}
esttab * using interaction_con_ESS.csv, csv replace star(+ 0.1 * 0.05) nogap ar2 pr2  se b(%9.2f) drop(*essround)  label

